package com.sangeng.controller;

import com.sangeng.annotation.SystemLog;
import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.dto.TagListDto;
import com.sangeng.domain.entity.Tag;
import com.sangeng.domain.vo.TagListVo;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.handler.exception.SystemException;
import com.sangeng.service.TagService;
import com.sangeng.utils.BeanCopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 标签(Tag)表控制层
 *
 * @author makejava
 * @since 2024-08-20 21:49:20
 */
@RestController
@RequestMapping("/content/tag")
@Api(tags = "标签管理")
public class TagController {
    /**
     * 服务对象
     */
    @Autowired
    private TagService tagService;



    @SystemLog(businessName = "标签管理")
    @GetMapping("/list")
    @ApiOperation(value = "标签列表")

    public ResponseResult list(Long pageNum , Long pageSize, TagListDto tagListDto)
    {
        return  tagService.pageTagList(pageNum , pageSize,  tagListDto);
    }

    //添加tag
    @SystemLog(businessName = "添加标签")
    @PostMapping()
    public ResponseResult addTag(@RequestBody TagListDto tagListDto){
            return tagService.addTag(tagListDto);
    }
    //删除tag
    @SystemLog(businessName = "删除tag")
    @DeleteMapping("/{id}")
    public ResponseResult deleteTag(@PathVariable Long id){
        tagService.removeById(id);
        return ResponseResult.okResult();
    }

    //通过id获取tag
    @SystemLog(businessName = "通过id获取tag")
    @GetMapping("/{id}")
    public ResponseResult getById(@PathVariable Long id){
        Tag tag = tagService.getById(id);
        TagListDto tagListDto = null;
        try {
            tagListDto = BeanCopyUtils.copyBean(tag, TagListDto.class);
        } catch (Exception e) {
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        return  ResponseResult.okResult(tagListDto);
    }

    //修改tag
    @SystemLog(businessName = "修改tag")
    @PutMapping()
    public ResponseResult updateTag(@RequestBody TagListDto tagListDto){
       return  tagService.updateByTagId(tagListDto);
    }

    //获取所有tag
    @SystemLog(businessName = "查询所有标签接口")
    @GetMapping("/listAllTag")
    public ResponseResult listAllTag(){
        List<Tag> list = tagService.list();
        List<TagListVo> tagListVos = BeanCopyUtils.copyBeanList(list, TagListVo.class);
        return ResponseResult.okResult(tagListVos);
    }

}

